﻿#include "SpecialPriceSeacher.h"
#include "MarketDataStream.h"
#include "Tools.h"
#include <iostream>

SpecialPriceSeacher::SpecialPriceSeacher(QObject *parent)
	: QObject(parent)
{
}

SpecialPriceSeacher::~SpecialPriceSeacher()
{
}

void SpecialPriceSeacher::FindDiffValueOpenPrice(const std::string &pSymbolName, const std::string &pDay)
{
	MDIO::Price priceStream;
	std::deque<MData::PriceItem> pPrices;
	priceStream.GetAllPrice(pDay, pSymbolName, pPrices);

	tm pdayzero = Tools::DateStr2TM(pDay);
	time_t beginTimestamp = Tools::GetTimeStamp(pdayzero);
	tm pnextdayzero = Tools::AddDays(pdayzero, 1);
	time_t endTimestamp = Tools::GetTimeStamp(pnextdayzero);
	int frmIndex = 1;
	for (time_t curtimestamp = beginTimestamp; curtimestamp < endTimestamp;)
	{
		findDiffOpenPrice(pPrices, curtimestamp);
	}
}

void SpecialPriceSeacher::findDiffOpenPrice(std::deque<MData::PriceItem> &todayPriceList, time_t &pDataTime)
{
	for (int i = 1; i < todayPriceList.size() - 1; ++i)
	{
		MData::PriceItem &tmpItem = todayPriceList.at(i);
		if (tmpItem.UpdateTime >= pDataTime)
		{
			// 上一根K线
			MData::PriceItem &lastPItem = todayPriceList.at(i - 1);
			if (lastPItem.LastPrice != tmpItem.LastPrice)
			{
				std::string dstTm = Tools::DateTimeStamp2Str(tmpItem.UpdateTime);
				std::cout << dstTm << " 价格与上一个行情价格不同" << std::endl;
			}
			
			// 下一根行情
			MData::PriceItem &nextPItem = todayPriceList.at(i + 1);
			tm tmDataTime = Tools::GetDateTimeFrmStamp(nextPItem.UpdateTime);
			tm itemStart = tmDataTime;
			itemStart.tm_min = (tmDataTime.tm_min / 5) * 5;
			itemStart.tm_sec = 0;
			time_t outKItemBegin = Tools::GetTimeStamp(itemStart);
			time_t outKItemEnd = outKItemBegin + 300;
			pDataTime = outKItemEnd;

			break;
		}
	}
}
